package com.deyuan.controoler;

import com.deyuan.bean.Student;
import com.deyuan.bean.StudentVo;
import com.deyuan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentControoler {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/queryById")
    public ModelAndView queryById(int id) {
        Student student = studentService.queryById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("student", student);
        modelAndView.setViewName("/update.jsp");
        return modelAndView;

    }

    @RequestMapping("/add")
    public ModelAndView add(Student student, HttpServletRequest request) {
        int addResult = studentService.add(student);
        ModelAndView modelAndView = new ModelAndView();
        if (addResult > 0) {
            modelAndView.addObject("msg", "添加成功");

            modelAndView.setViewName("/index.jsp");
        } else {
            modelAndView.addObject("msg", "添加失败");
            //添加失败，跳回到添加页面
            modelAndView.addObject("student", student);
            modelAndView.setViewName("/add.jsp");
        }
        return modelAndView;
    }

    @RequestMapping("/queryAll")
    public ModelAndView queryAll() {
        List<Student> students = studentService.queryAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", students);
        modelAndView.setViewName("/list.jsp");
        return modelAndView;
    }

    @RequestMapping("/delete")
    public ModelAndView delete(int[] id) {
        StudentVo studentVo = new StudentVo();
        List<Integer> ids = new ArrayList<>();
        for (int i : id) {
            ids.add(i);
        }
        studentVo.setIds(ids);
        studentService.delete(studentVo);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "删除成功");
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

    @RequestMapping("/queryByName")
    public ModelAndView queryByName(String name) {
        List<Student> students = studentService.queryByName(name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", students);
        modelAndView.setViewName("/list.jsp");
        return modelAndView;
    }

    @RequestMapping("/update")
    public ModelAndView update(Student student) {
        studentService.update(student);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("queryAll");
        return modelAndView;
    }
}
